package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;


    /**
     * 添加"品类"
     * @param session
     * @param categoryName
     * @param parentId
     * @return
     */
    @RequestMapping(value = "add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName,
                                      @RequestParam(value = "parentId", defaultValue = "0") int parentId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }

        if(iUserService.checkAdminRole(user).isSuccess()){
            return iCategoryService.addCategory(categoryName, parentId);
        }else{
            return ServerResponse.createByErrorMessage("您无权操作,需要管理员权限");
        }
    }

    /**
     * 修改"品类"名称
     * @param session
     * @param categoryId
     * @param categroyName
     * @return
     */
    @RequestMapping(value = "set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session, Integer categoryId, String categroyName){
        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            return iCategoryService.updateCategoryName(categoryId,categroyName);
        }else{
            return adminResponse;
        }
    }

    /**
     * 查询子节点
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,
                                                      @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            // 查询子节点的category信息,保持平级,不递归
            return iCategoryService.getChildrenParallelCategory(categoryId);
        }else{
            return adminResponse;
        }
    }

    /**
     * 查询子节点,递归
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,
                                                      @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        ServerResponse adminResponse = checkAdminRole(session);
        if(adminResponse.isSuccess()){
            // 查询当前节点的id并且递归当前子节点的id
            return iCategoryService.selectCategoryAndChildrenById(categoryId);
        }else{
            return adminResponse;
        }
    }


    /**
     * 私有方法, 抽取重复操作
     * 1: 验证用户是否登录; 2: 验证是否具有管理员权限
     *
     * @param session
     * @return
     */
    private ServerResponse checkAdminRole(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        return iUserService.checkAdminRole(user);
    }
}
